package jwt;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.faces.event.ActionEvent;
import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;


@SuppressWarnings({"unchecked"})
public class JPABean {

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPA_PU");
    EntityManager em = emf.createEntityManager();

    public List<UzytkownikEntity> getListUzytkownik() {
        return em.createQuery("select u from UzytkownikEntity u").getResultList();
    }

    public List<ProfilEntity> getListProfil() {
        return em.createQuery("select p from ProfilEntity p").getResultList();
    }

    public SelectItem[] getProfilItems() {
        List<ProfilEntity> list = getListProfil();
        SelectItem[] ret = new SelectItem[list.size()];
        int i = 0;
        for (ProfilEntity profilEntity : list) {
            ret[i++] = new SelectItem(profilEntity.getNazwa()); 
        }
        return ret;
    }

    private String nowyProfil;

    public String getNowyProfil() {
        return nowyProfil;
    }

    public void setNowyProfil(String nowyProfil) {
        this.nowyProfil = nowyProfil;
    }

    public String dodajProfil() {
        ProfilEntity pe = new ProfilEntity();
        pe.setNazwa(nowyProfil);
        persist(pe);
        return null;
    }

    public void usunProfil(ActionEvent event) {
        Map session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        UIParameter component = (UIParameter) event.getComponent().findComponent("deleteId");
        ProfilEntity pe = (ProfilEntity) component.getValue();
        em.getTransaction().begin();
        em.remove(pe);
        em.getTransaction().commit();
    }

    public void usunUsera(ActionEvent event) {
        Map session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        UIParameter component = (UIParameter) event.getComponent().findComponent("deleteId");
        UzytkownikEntity pe = (UzytkownikEntity) component.getValue();
        em.getTransaction().begin();
        em.remove(pe);
        em.getTransaction().commit();
    }

    public void persist(Object object) {
        em.getTransaction().begin();
        try {
            em.persist(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        }
    }

    private String nowyUserNazwa, nowyUserProfil;

    public String getNowyUserProfil() {
        return nowyUserProfil;
    }

    public void setNowyUserProfil(String nowyUserProfil) {
        this.nowyUserProfil = nowyUserProfil;
    }

    public String getNowyUserNazwa() {
        return nowyUserNazwa;
    }

    public void setNowyUserNazwa(String nowyUserNazwa) {
        this.nowyUserNazwa = nowyUserNazwa;
    }

    public String dodajUsera() {
        UzytkownikEntity ue = new UzytkownikEntity();
        ue.setNazwa(nowyUserNazwa);
        ue.setProfil(nowyUserProfil);
        persist(ue);
        return null;
    }
}
